<template>
	<view class="" @tap="upload" style="position: relative">
		<slot></slot>
	</view>
</template>

<script>
	import api from '@/common/api.js';
	import global from '@/common/global.js'
	import {
		mapGetters
	} from 'vuex'
	export default {
		computed: {
			...mapGetters(['token'])
		},
		props: {

		},
		data() {
			return {
				isupload: 0
			}
		},
		methods: {
			previewImage(res) {
				console.log(1)
				uni.previewImage({
					urls: [res.url],
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},

			upload() {
				if (this.isupload === 1) {
					return
				}
				let thit = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed '], //可以指定是原图还是压缩图，默认二者都有
					success: function(chooseImageRes) {
						thit.isupload = 1
						console.log(chooseImageRes)
						const tempFilePaths = chooseImageRes.tempFilePaths;
						const uploadTask = uni.uploadFile({
							url: global.baseUrl + api.file.cardPosUpload, //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								"X-Access-Token": thit.token,
								"content-type":"multipart/form-data"

							},
							formData:{
								isFront:1
							},
							success: (uploadFileRes) => {
								let json = JSON.parse(uploadFileRes.data)
								if (json.success) {
									thit.$emit("success", {
										value: json,
										url: thit.$getFile(json.message)
									})
								}
							},
						});
						// console.log(JSON.stringify(res.tempFilePaths));
					}
				});
			}
		}
	}
</script>

<style lang="scss">
</style>
